package light.head.controller.el;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import light.head.constant.Msg;
import light.head.constant.Parm;
import light.head.constant.Url;
import light.head.dao.el.CourseDAO;
import light.head.dao.el.FrontCourseDAO;
import light.head.dao.el.LecturerDAO;
import light.head.dao.sys.OrganDAO;
import light.head.enm.RESULT;
import light.head.framework.base.SupporterController;
import light.head.framework.entity.PageBean;
import light.head.model.el.Course;
import light.head.model.el.CourseLecturer;
import light.head.model.el.FrontCourse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
/**
 * 课程管理
 */
@Controller
@RequestMapping(value = Url.C_COURSE)
public class CourseController extends SupporterController{
	
	private CourseDAO courseDAO;
	private FrontCourseDAO fcDAO;
	private LecturerDAO leDAO;
	private OrganDAO organDAO;

	@RequestMapping(Url.INIT)
	public ModelAndView init(HttpServletRequest request, HttpServletResponse response){
		return new ModelAndView(Url.COURSE_INIT);
	}
	
	@RequestMapping(Url.QUERY)
	public ModelAndView query(HttpServletRequest request, HttpServletResponse response){
		PageBean pb = createPageBean(request);
		Map<String, String> map = createQueryMapByParmName(request, Parm.NAME, Parm.DELETED);
		Integer oc = organDAO.getRoot(cu_organCode(request)).getCode();
		if (courseDAO.findByMap(oc, map, pb) == RESULT.SUCCESS){
			Course c =null;
			for (Object o : pb.getResultList()) {
				c = (Course) o;
				List<FrontCourse> list = fcDAO.findCc(c.getCode());
				c.setItemList(list);
			}
		}
		request.setAttribute(Parm.PAGE_BEAN, pb);
		return new ModelAndView(Url.COURSE_LIST);
	}
	
	@RequestMapping(Url.EDIT)
	public ModelAndView edit(HttpServletRequest request, HttpServletResponse response){
		String code = request.getParameter(Parm.CODE);
		if(isNotNull(code)){
			Course course = courseDAO.get(Integer.parseInt(code));
			List<FrontCourse> list = fcDAO.findCc(course.getCode());
			List<CourseLecturer> list1 = fcDAO.findLc(course.getCode());
			course.setItemList(list);
			course.setLcList(list1);
			request.setAttribute(Parm.COURSE, course);
		}
		return new ModelAndView(Url.COURSE_EDIT);
	}
	
	
	@RequestMapping(Url.SAVE)
	@Transactional(rollbackFor = Exception.class)
	public void save(Course course, HttpServletRequest request, HttpServletResponse response){
		RESULT result = RESULT.ERROR;
		Integer oc = organDAO.getRoot(cu_organCode(request)).getCode();
		if(course.getCode() == null){
			List<Course> clist = courseDAO.findname(oc, course.getName());
			if (clist != null && clist.size() != 0) {
				print(response, Msg.EXISTS);	
				return;
			}
			Date ct = new Date();
			course.setCt(ct);
			course.setOc(oc);
			course.setCu(cu_code(request));
			course.setDeleted(false);
			result = courseDAO.add(course);
			if (course.getItemList() != null && course.getItemList().size() > 0){
				for (FrontCourse fc : course.getItemList()) {
					if(fc.getFc() != null ){
						fc.setCc(course.getCode());
						courseDAO.add(fc);
					}
				}
			}
			if (course.getLcList() != null && course.getLcList().size() > 0){
				for (CourseLecturer lc : course.getLcList()) {
					if(lc.getLc() != null){
						lc.setOc(oc);
						lc.setCc(course.getCode());
						courseDAO.add(lc);
					}
				}
			}
		}else{
			List<Course> clist = courseDAO.findname(oc, course.getName());
			if (clist == null || clist.size() == 0) {
				Course c = courseDAO.get(course.getCode());
				c.setName(course.getName());
				result = courseDAO.upd(c);
			}
			if (course.getItemList() != null && course.getItemList().size() > 0) {
				List<FrontCourse> list = course.getItemList();						//页面上传过来的联系人
				List<FrontCourse> oldList = fcDAO.findCc(course.getCode());		//数据库里的联系人
				List<FrontCourse> deletedList = new ArrayList<FrontCourse>();	// 存放页面已经删除的角色节点
				Boolean yes = null;
				for (FrontCourse db : oldList) {
					yes = true;
					if (course.getItemList() == null || course.getItemList().size() == 0) {
						deletedList.add(db);
					} else {
						for (FrontCourse ibc : list) {
							if (null != ibc.getCode() && ibc.getCode().intValue() == db.getCode().intValue()) {
								yes = false;
								break;
							} 
						}
						if (yes) {
							deletedList.add(db);
						}
					}
				}
				// 删除(在数据库中deleted = true)
				for (FrontCourse ua : deletedList) {
					result = fcDAO.del(ua);
				}
				for (FrontCourse pii : list) {
					if (null == pii.getCode()) {
						if (null != pii.getFc()) {
							pii.setCc(course.getCode());
							result = courseDAO.add(pii);
						}
					} else {
						for (FrontCourse old : oldList) {
							if (pii.getCode().intValue() == old.getCode().intValue()) {
								old.setFc(pii.getFc());
								old.setCc(pii.getCc());
								result = courseDAO.upd(old);
							}
						}
					} 
				}
			}
			if (course.getLcList() != null && course.getLcList().size() > 0) {
				List<CourseLecturer> list = course.getLcList();						//页面上传过来的联系人
				List<CourseLecturer> oldList = fcDAO.findLc(course.getCode());		//数据库里的联系人
				List<CourseLecturer> deletedList = new ArrayList<CourseLecturer>();	// 存放页面已经删除的角色节点
				Boolean yes = null;
				for (CourseLecturer db : oldList) {
					yes = true;
					if (course.getLcList() == null || course.getLcList().size() == 0) {
						deletedList.add(db);
					} else {
						for (CourseLecturer ibc : list) {
							if (null != ibc.getCc() && ibc.getCc().intValue() == db.getCc().intValue() && null != ibc.getLc() && ibc.getLc().intValue() == db.getLc().intValue()) {
								yes = false;
								break;
							} 
						}
						if (yes) {
							deletedList.add(db);
						}
					}
				}
				// 删除(在数据库中deleted = true)
				for (CourseLecturer ua : deletedList) {
					result = fcDAO.del(ua);
				}
				for (CourseLecturer pii : list) {
					if (null == pii.getCc()) {
						if (null != pii.getLc()) {
							pii.setOc(oc);
							pii.setCc(course.getCode());
							result = courseDAO.add(pii);
						}
					} else {
						for (CourseLecturer old : oldList) {
							if (pii.getCc().intValue() == old.getCc().intValue()) {
								old.setLc(pii.getLc());
								old.setCc(pii.getCc());
								result = courseDAO.upd(old);
							}
						}
					}
				}
			}
		}
		if(RESULT.SUCCESS == result){
			courseDAO.loadAll(); //重新加载缓存
			print(response, Msg.SUCCESS);
		}else{
			print(response, Msg.ERROR);	
		}
	}
	
	@RequestMapping(Url.SHIELD)
	public void shield(HttpServletRequest request, HttpServletResponse response){
		String code = request.getParameter(Parm.CODE);
		RESULT result = RESULT.ERROR;
		if(isNotNull(code)){
			Course c = courseDAO.get(Integer.parseInt(code));
			if(c != null){
				c.setDeleted(true);
				result = courseDAO.upd(c);
			}
		}
		if(result == RESULT.SUCCESS){
			courseDAO.loadAll(); //重新加载缓存
			print(response, Msg.SUCCESS);
		}else{
			print(response, Msg.ERROR);
		}
	}
	
	@RequestMapping(Url.UN_SHIELD)
	public void unshield(HttpServletRequest request, HttpServletResponse response){
		String code = request.getParameter(Parm.CODE);
		RESULT result = RESULT.ERROR;
		if(isNotNull(code)){
			Course c = courseDAO.get(Integer.parseInt(code));
			if(c != null){
				c.setDeleted(false);
				result = courseDAO.upd(c);
			}
		}
		if(result == RESULT.SUCCESS){
			courseDAO.loadAll(); //重新加载缓存
			print(response, Msg.SUCCESS);
		}else{
			print(response, Msg.ERROR);
		}
	}
	//选择课程
	@RequestMapping(Url.AID_INIT)
	public ModelAndView aidInit(HttpServletRequest request, HttpServletResponse response){
		return new ModelAndView(Url.COURSE_ADDINIT);
	}
	
	@RequestMapping(Url.AID_LIST)
	public ModelAndView aidList(HttpServletRequest request, HttpServletResponse response){
		PageBean pb = createPageBean(request);
		Map<String, String> map = createQueryMapByParmName(request, "codes", "cn");
		courseDAO.aidFindByMap(cu_organCode(request), map, pb);
		request.setAttribute(Parm.PAGE_BEAN, pb);
		return new ModelAndView(Url.COURSE_ADDLIST);
	}
	
	/**
	 * 查询讲师
	 */
	@RequestMapping(Url.AID_INIT_1)
	public ModelAndView aidUserInit(HttpServletRequest request, HttpServletResponse response) throws Exception {
		request.setAttribute(Parm.A, request.getParameter(Parm.A));
		request.setAttribute(Parm.P, request.getParameter(Parm.P));
		return new ModelAndView(Url.COURSE_AIDINIT);
	}

	@RequestMapping(Url.AID_LIST_1)
	public ModelAndView aidUserList(HttpServletRequest request, HttpServletResponse response) throws Exception {
		PageBean pageBean = createPageBean(request);
		Map<String, String> map = createQueryMapByParmName(request, Parm.LN, Parm.SUC);
		Integer oc = organDAO.getRoot(cu_organCode(request)).getCode();
		if (RESULT.SUCCESS == leDAO.findLc(oc, map, pageBean)) {
			request.setAttribute(Parm.PAGE_BEAN, pageBean);
		}
		return new ModelAndView(Url.COURSE_AIDLIST);
	}
	
	
	@Autowired
	public void setCourseDAO(CourseDAO courseDAO) {
		this.courseDAO = courseDAO;
	}
	@Autowired
	public void setOrganDAO(OrganDAO organDAO) {
		this.organDAO = organDAO;
	}
	@Autowired
	public void setFcDAO(FrontCourseDAO fcDAO) {
		this.fcDAO = fcDAO;
	}
	@Autowired
	public void setLeDAO(LecturerDAO leDAO) {
		this.leDAO = leDAO;
	}
	
}
